<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Safety extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
    }
	

	public function index() {
        $this->load->view('m/safety.php');
    }
    
    
    public function edit() {
        if($this->input->is_post()) {
            $that_userid = $this->thatUser['id'];
            
			//验证规则
			$config = array(
                array(
                    'field'   => 'password', 
                    'label'   => '密码', 
                    'rules'   => 'trim|required|min_length[6]|max_length[20]'
                ),
                array(
                    'field' => 'password_again',
                    'label' => '重复密码',
                    'rules' => 'trim|required|min_length[6]|max_length[20]'
                ),
            );

            $this->form_validation->set_rules($config);

			if ($this->form_validation->run() === TRUE)
  			{
  				$password = $this->input->post('password');
                $pwdAgain = $this->input->post('password_again');
                   
                //验证密码是否和未改之前一样
                $userInfo = $this->User_model->get_info_by_id($that_userid);
                if(($password == $pwdAgain) && ($userInfo['password'] != md5($password))) {
                     //密码加密
                    $plaintext = md5($password);

                    $data = array('password'=>$plaintext);
                    //保存数据库

                    $this->User_model->update_by_where(array('id'=>$that_userid), $data);

                    redirect(base_url('/user/login'));
                }
                
                /* 异常处理 */
                if($password != $pwdAgain) {
                    $data['error'] = '两次输入的密码不一致，请重新填写';
                }
                
                //验证密码是否和未改之前一样
                if($userInfo['password'] ==  md5($password)) {
                    $data['error'] = '密码与最近一次更新一致，无需修改';
                }
                
                $this->load->view('/m/safety', $data);
			}
        }
    }
}